var t = require("../../../../@babel/runtime/helpers/defineProperty"), s = getApp();

Page({
    data: {
        language: "",
        checked: -1,
        activeName: "first",
        posterShow: !1,
        posterImg: "",
        goods_list: [],
        datas: {},
        id: "",
        cservice: "",
        share_title: "",
        share_icon: "",
        share_desc: "",
        selected_goods: {},
        isGoods: !0,
        specsManage: [],
        SUBMIT_ACTION_CART: "-1",
        SUBMIT_ACTION_BUY: "-2",
        submitActionTag: "-2",
        optionsId: "",
        goodsCount: 1,
        optionsMaxCount: 1,
        popupSpecs: !1,
        popupSpecs2: !1,
        popStock: 0,
        popPrice: 0,
        goodsDescription: ""
    },
    onLoad: function(t) {
        if (t.scene) {
            var a = decodeURIComponent(t.scene);
            if (a) {
                var o = [];
                o = a.split(",");
                for (var e = 0; e < o.length; e++) {
                    var i;
                    i = o[e].split("="), console.log(i), "id" == i[0] ? this.setData({
                        checked: -1,
                        id: i[1]
                    }) : "mid" == i[0] && s._setMid(i[1]);
                }
            }
        } else t.id && this.setData({
            checked: -1,
            id: t.id
        }), t.mid && s._setMid(t.mid);
        this.getData();
    },
    saveImg: function() {
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.writePhotosAlbum"] || wx.authorize({
                    scope: "scope.writePhotosAlbum",
                    success: function() {
                        console.log("授权成功");
                    }
                });
            }
        });
        var t = this.data.posterImg.image_url;
        wx.downloadFile({
            url: t,
            success: function(t) {
                console.log(t), wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.showToast({
                            title: "保存成功",
                            icon: "none",
                            duration: 2e3
                        });
                    },
                    fail: function(t) {
                        console.log(t), "saveImageToPhotosAlbum:fail auth deny" === t.errMsg && (console.log("当初用户拒绝，再次发起授权"), 
                        wx.openSetting({
                            success: function(t) {
                                t.authSetting["scope.writePhotosAlbum"] ? wx.showToast({
                                    icon: "none",
                                    title: "获取权限成功,再次长按保存图片",
                                    duration: 1500
                                }) : wx.showToast({
                                    icon: "none",
                                    title: "获取权限失败,保存图片需要用户授权",
                                    duration: 1500
                                });
                            }
                        }));
                    },
                    complete: function(t) {
                        console.log(t);
                    }
                });
            }
        });
    },
    submitAction: function() {
        var t = this.data.specsManage, s = this.data.submitActionTag, a = this.data.SUBMIT_ACTION_BUY;
        if (t.length < this.data.goods_list[this.data.checked].has_many_specs.length) return this.setData({
            popupSpecs: !1
        }), void wx.showToast({
            icon: "none",
            title: this.data.goodsDescription,
            duration: 1500
        });
        s == a && (this.setData({
            popupSpecs: !1
        }), this.buyNowRequest(this.data.selected_goods.id, this.data.optionsId, this.data.goodsCount));
    },
    buyNowNew: function() {
        this.popupSpecs2CloseBtn(), this.buyNowRequest(this.data.selected_goods.id, this.data.optionsId, this.data.goodsCount);
    },
    reduceGoods: function() {
        var t = this.data.goodsCount;
        1 != this.data.goodsCount && 0 != this.data.goodsCount && this.setData({
            goodsCount: t - 1
        });
    },
    addGoods: function() {
        var t = this.data.goodsCount, s = this.data.specsManage, a = this.data.optionsMaxCount;
        if (console.log("增加"), s.length == this.data.goods_list[this.data.checked].has_many_specs.length && this.data.goodsCount == a) return console.log("数量超出范围"), 
        wx.showToast({
            icon: "none",
            title: "数量超出范围",
            duration: 1500
        }), void console.log("max=" + this.data.goodsCount);
        this.setData({
            goodsCount: t + 1
        });
    },
    inputGoodsNum: function(t) {
        var a = parseInt(t.detail.value), o = this.data.goodsCount;
        8 == t.detail.keyCode && s._isTextEmpty(t.detail.value) ? this.setData({
            goodsCount: ""
        }) : a ? this.setData({
            goodsCount: a
        }) : this.setData({
            goodsCount: o
        });
    },
    buyNowRequest: function(t, a, o) {
        if (0 != this.data.optionsMaxCount) if (o <= 0) wx.showToast({
            icon: "none",
            title: "请选择商品数量",
            duration: 1500
        }); else {
            var e = this, i = [];
            i.push(t);
            var n = s.getNetAddresss("from.div-from.isDisplay");
            n += "&goods_ids=" + JSON.stringify(i), s._getNetWork({
                showToastIn: !1,
                url: n,
                success: function(s) {
                    var i = s.data;
                    if (1 == i.result) {
                        var n = i.data;
                        console.log("xxxx", n), i.data.status && !i.data.member_status ? wx.showModal({
                            title: "提示",
                            content: "购买此商品,请补充您的个人信息",
                            success: function(t) {
                                t.confirm ? wx.navigateTo({
                                    url: "/packageB/member/address/myinfo/myinfo?tag=delta"
                                }) : t.cancel && console.log("用户点击取消");
                            }
                        }) : wx.navigateTo({
                            url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=" + e.data.submitActionTag + "&goodsId=" + t + "&optionsId=" + a + "&total=" + o + "&activity_id=" + e.data.id
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: i.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {}
            });
        } else wx.showToast({
            icon: "none",
            title: "商品库存不足",
            duration: 1500
        });
    },
    goMember: function() {
        wx.reLaunch({
            url: "/packageG/member_v2/member_v2"
        });
    },
    goHome: function() {
        wx.reLaunch({
            url: "/packageG/index/index"
        });
    },
    posterShowCloseBtn: function() {
        this.setData({
            posterShow: !1
        });
    },
    getData: function() {
        var t = this, a = s.getNetAddresss("plugin.commission-activity.api.activity.detail");
        s._postNetWork({
            url: a,
            showToastIn: !1,
            data: {
                id: this.data.id
            },
            success: function(s) {
                var a = s.data;
                if (1 == a.result) {
                    var o = a.data;
                    t.setData({
                        datas: o,
                        goods_list: o.goods,
                        share_title: o.share_title,
                        share_icon: o.share_icon,
                        share_desc: o.share_desc
                    });
                } else wx.showModal({
                    title: "提示",
                    content: a.msg,
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.navigateBack({
                            delta: 1,
                            fail: function() {
                                wx.reLaunch({
                                    url: "/packageG/index/index"
                                });
                            }
                        });
                    }
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    initGoods: function() {
        this.data.specsManage = [], this.data.optionsId = "", this.data.optionsMaxCount = 1, 
        this.setData({
            popStock: 0,
            popPrice: 0,
            goodsDescription: "",
            goodsCount: 1
        });
    },
    buyNow: function() {
        if (-1 == this.data.checked) return wx.showToast({
            icon: "none",
            title: "请选择要购买的商品",
            duration: 1500
        }), !1;
        if (this.initGoods(), this.setData({
            selected_goods: this.data.goods_list[this.data.checked]
        }), this.data.optionsMaxCount = this.data.selected_goods.stock, this.data.isGoods) if (1 == this.data.goods_list[this.data.checked].has_option) {
            if (this.setData({
                popupSpecs: !0
            }), !this.data.optionsId) for (var s = 0; s < this.data.goods_list[this.data.checked].has_many_specs.length; s++) this.setData(t({}, "goods_list[" + this.data.checked + "].has_many_specs[" + s + "].description", this.data.goods_list[this.data.checked].has_many_specs[s].has_many_specs_item[0])), 
            this.selectSpecs(this.data.goods_list[this.data.checked].has_many_specs[s].has_many_specs_item[0], "first", s);
        } else 0 == this.data.goods_list[this.data.checked].has_option && this.showPopView2(); else wx.showToast({
            icon: "none",
            title: "商品已下架或不存在",
            duration: 1500
        });
    },
    showPopView2: function() {
        this.setData({
            popupSpecs2: !0
        });
    },
    popupSpecsCloseBtn: function() {
        this.setData({
            popupSpecs: !1
        });
    },
    popupSpecs2CloseBtn: function() {
        this.setData({
            popupSpecs2: !1
        });
    },
    selectSpecs: function(s, a, o) {
        var e = {}, i = 0;
        if ("first" == a ? (i = o, e = s) : (i = s.currentTarget.dataset.specsidex, e = s.currentTarget.dataset.specitem), 
        e.c) return !1;
        this.setData(t({}, "goods_list[" + this.data.checked + "].has_many_specs[" + i + "].description", e.id)), 
        this.manageSpecs(e), this.setGoodsSpecs(e), this.setGoodsSpecsChangeInfo(), this.getMaxCount();
    },
    getMaxCount: function() {
        var t = this.data.specsManage, s = this.data.optionsMaxCount;
        t.length == this.data.goods_list[this.data.checked].has_many_specs.length && (0 == s && this.setData({
            goodsCount: 0
        }), this.data.goodsCount > s && this.setData({
            goodsCount: s
        }));
    },
    manageSpecs: function(t) {
        var s = new Object(), a = this.data.specsManage;
        if (s.id = t.id, s.specid = t.specid, s.title = t.title, a.length > 0) {
            for (var o = 0; o < a.length; o++) a[o].specid == s.specid && a.splice(o, 1);
            a.push(s);
        } else a.push(s);
        if (a.length == this.data.goods_list[this.data.checked].has_many_specs.length) {
            for (var e = [], i = 0; i < this.data.goods_list[this.data.checked].has_many_specs.length; i++) for (var n = 0; n < a.length; n++) if (this.data.goods_list[this.data.checked].has_many_specs[i].id == a[n].specid) {
                e.push(a[n]);
                break;
            }
            a = e;
        }
        this.setData({
            specsManage: a
        }), this.setGoodsDescription();
    },
    setGoodsDescription: function() {
        var t = "", s = this.data.specsManage;
        if (s.length == this.data.goods_list[this.data.checked].has_many_specs.length) {
            t = "已选择 ";
            for (var a = 0; a < s.length; a++) t += s[a].title + " ";
            this.setData({
                goodsDescription: t
            });
        } else {
            t = "请选择 ";
            for (var o = 0; o < this.data.goods_list[this.data.checked].has_many_specs.length; o++) for (var e = 0; e < s.length; e++) if (this.data.goods_list[this.data.checked].has_many_specs[o].id != s[e].specid) {
                t += this.data.goods_list[this.data.checked].has_many_specs[o].title + " ";
                break;
            }
            this.setData({
                goodsDescription: t
            });
        }
    },
    setGoodsSpecs: function(s) {
        for (var a = 0; a < this.data.goods_list[this.data.checked].has_many_specs.length; a++) if (s.specid != this.data.goods_list[this.data.checked].has_many_specs[a].id) {
            var o = this.setGoodsSpecsStatus(this.data.goods_list[this.data.checked].has_many_specs[a].specitem, s.id);
            this.setData(t({}, "goods_list[" + this.data.checked + "].has_many_specs[" + a + "].specitem", o));
        }
    },
    setGoodsSpecsStatus: function(t, s) {
        for (var a = [], o = 0; o < this.data.goods_list[this.data.checked].has_many_options.length; o++) for (var e = this.data.goods_list[this.data.checked].has_many_options[o].specs.split("_"), i = 0; i < e.length; i++) if (e[i] == s) return void a.push(this.data.goods_list[this.data.checked].has_many_options[o]);
        for (var n = 0; n < a.length; n++) for (var d = a[n].specs.split("_"), c = 0; c < d.length; c++) if (d[c] != s && 0 == a[n].stock) for (var h = 0; h < t.length; h++) d[c] == t[h].id && (t[h].c = !0); else if (d[c] != s && a[n].stock > 0) for (var r = 0; r < t.length; r++) d[c] == t[r].id && (t[r].c = !1);
        return t;
    },
    setGoodsSpecsChangeInfo: function() {
        var t = this.data.specsManage;
        if (t.sort(function(t, s) {
            return t.id - s.id;
        }), t.length == this.data.goods_list[this.data.checked].has_many_specs.length) {
            for (var a = "", o = 0; o < t.length; o++) a += t[o].id + "_";
            a = a.substring(0, a.length - 1);
            for (var e = 0; e < this.data.goods_list[this.data.checked].has_many_options.length; e++) if (a == this.setGoodsSpecsBySort(this.data.goods_list[this.data.checked].has_many_options[e].specs)) {
                this.setData({
                    popPrice: this.data.goods_list[this.data.checked].has_many_options[e].product_price,
                    popThumb: s._isTextEmpty(this.data.goods_list[this.data.checked].has_many_options[e].thumb) ? this.data.goods_list[this.data.checked].thumb : this.data.goods_list[this.data.checked].has_many_options[e].thumb,
                    popStock: this.data.goods_list[this.data.checked].has_many_options[e].stock,
                    optionsId: this.data.goods_list[this.data.checked].has_many_options[e].id
                }), this.data.optionsMaxCount = this.data.goods_list[this.data.checked].has_many_options[e].stock, 
                this.data.optionsMaxCount > 0 && this.setData({
                    goodsCount: 1
                });
                break;
            }
        }
        this.setData({
            specsManage: t
        });
    },
    setGoodsSpecsBySort: function(t) {
        var s = t.split("_");
        s.sort(function(t, s) {
            return t - s;
        });
        for (var a = "", o = 0; o < s.length; o++) a += s[o] + "_";
        return a = a.substring(0, a.length - 1);
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), s = "";
        return t && (s = t), {
            path: "/packageB/member/distribution_queue/DistributionActivityDetail/DistributionActivityDetail?id=" + this.data.id + "&mid=" + s
        };
    },
    onChange: function(t) {
        var s = t.detail;
        this.setData({
            checked: Number(s)
        });
    },
    postShow: function() {
        if (this.data.posterImg) this.setData({
            posterShow: !0
        }); else {
            var t = this, a = s.getNetAddresss("plugin.commission-activity.api.activity-poster.index");
            a += "&id=" + this.data.id, s._getNetWork({
                url: a,
                success: function(s) {
                    var a = s.data;
                    1 == a.result ? t.setData({
                        posterImg: a.data,
                        posterShow: !0
                    }) : (t.setData({
                        posterShow: !1
                    }), wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    }));
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    gotoGoods: function(t) {
        var s = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + s + "&activity_id=" + this.data.id
        });
    }
});